﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using Happy.Domain;

namespace Happy.Activities.Domain.WorkflowInstances
{
    /// <summary>
    /// 活动实例。
    /// </summary>
    public class ActivityInstance : Entity<Guid>
    {
        /// <summary>
        /// 分流记号。
        /// </summary>
        public string SplitToken { get; internal set; }

        /// <summary>
        /// 活动状态。
        /// </summary>
        public ActivityState State { get; internal set; }

        /// <summary>
        /// 活动定义标识。
        /// </summary>
        public Guid ActivityId { get; internal set; }

        /// <summary>
        /// 流程实例标识。
        /// </summary>
        public Guid WorkflowInstanceId { get; internal set; }

        internal void SetStateToEntering(WorkflowContext context)
        {
            this.State = ActivityState.Entering;

            if (context.WorkflowInstance.OnActivityEntering != null)
            {
                context.WorkflowInstance.OnActivityEntering(context, this);
            }
        }

        internal void SetStateToEntered(WorkflowContext context)
        {
            this.State = ActivityState.Entered;

            if (context.WorkflowInstance.OnActivityEntered != null)
            {
                context.WorkflowInstance.OnActivityEntered(context, this);
            }
        }

        internal void SetStateToExiting(WorkflowContext context)
        {
            this.State = ActivityState.Exiting;

            if (context.WorkflowInstance.OnActivityExiting != null)
            {
                context.WorkflowInstance.OnActivityExiting(context, this);
            }
        }

        internal void SetStateToExited(WorkflowContext context)
        {
            this.State = ActivityState.Exited;

            if (context.WorkflowInstance.OnActivityExited != null)
            {
                context.WorkflowInstance.OnActivityExited(context, this);
            }
        }
    }
}
